<?php
if (!isset($_SESSION['nip'], $_SESSION['username'])) {
    header("Location: index.php?page=login");
}
?>

<?php include '_header.php'; ?>
<?php include '_menupanel.php'; ?>
<?php include '_menubar.php'; ?>

<script type="text/javascript">
    var url;
    var isUpdate;
    var isOpened;
    var id;

    function create() {
        isUpdate = false;
        $('#dialog-form').dialog('open').dialog('setTitle', 'Input Mahasiswa');
        $('#form').form('clear');
        $("#nim").focus();
    }
    function edit(rowData) {
        isUpdate = true;
        var row = rowData !== null ? rowData : $('#datagrid').datagrid('getSelected');
        if (row) {
            id = row.id;
            $('#dialog-form').dialog('open').dialog('setTitle', 'Edit Mahasiswa');
            $('#form').form('load', row);
            isOpened = true;
        }
    }
    function closeform() {
        $('#dialog-form').dialog('close');
        isOpened = false;
    }
    function save() {
        var kode = $("#nim").val();
        var nama = $("#nama").val();
        var form_data = $("#form").serialize();
        form_data = isUpdate ? form_data + "&id=" + id : form_data;
        
        if (kode.length === 0) {
            $("#nim").focus();
            return false();
        }
        if (nama.length === 0) {
            $("#nama").focus();
            return false();
        }

        url = isUpdate !== true ? "controller/msiswa_crud.php?act=add" : "controller/msiswa_crud.php?act=upd";

        $.ajax({
            type: "POST",
            url: url,
            data: form_data,
            success: function(data) {
                $.messager.show({
                    title: 'Message',
                    msg: data,
                    timeout: 2000,
                    showType: 'slide'
                });
                $('#datagrid').datagrid('reload');
                if (!isUpdate) {
                    $("#nim").focus();
                    $('#form').form('clear');
                }
            }
        });
    }
    function destroy() {
        var checked = [];
        var rows = $('#datagrid').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih" : rows[0].nim;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].nim;
        }

        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan menghapus ' + item + '?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/msiswa_crud.php?act=del",
                        data: 'nim=' + JSON.stringify(checked),
                        success: function(data) {
                            $.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 2000,
                                showType: 'slide'
                            });
                            $('#datagrid').datagrid('reload');
                        }
                    });
                }
            });
        }
    }

    function accept() {
        var checked = [];
        var rows = $('#datagrid2').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih" : rows[0].nim;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].id;
        }

        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan menerima pendaftaran ' + item + '?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/msiswa_temp_crud.php?act=add",
                        data: 'id=' + JSON.stringify(checked),
                        success: function(data) {
                            $.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 2000,
                                showType: 'slide'
                            });
                            $('#datagrid2').datagrid('reload');
                        }
                    });
                }
            });
        }
    }
    function denied() {
        var checked = [];
        var rows = $('#datagrid2').datagrid('getChecked');
        var item = rows.length > 1 ? "(" + rows.length + ") item yang terpilih" : rows[0].nim;
        for (var i = 0; i < rows.length; i++) {
            checked[i] = rows[i].id;
        }

        if (checked.length > 0) {
            $.messager.confirm('Confirm', 'Apakah Anda akan menolak pendaftaran ' + item + '?', function(option) {
                if (option) {
                    $.ajax({
                        type: "POST",
                        url: "controller/msiswa_temp_crud.php?act=del",
                        data: 'id=' + JSON.stringify(checked),
                        success: function(data) {
                            $.messager.show({
                                title: 'Message',
                                msg: data,
                                timeout: 2000,
                                showType: 'slide'
                            });
                            $('#datagrid2').datagrid('reload');
                        }
                    });
                }
            });
        }
    }

    function refresh() {
        $('#datagrid').datagrid('reload');
    }
    function doSearch(value) {
        $('#datagrid').datagrid('load', {
            cari: value
        });
    }
    function refresh2() {
        $('#datagrid2').datagrid('reload');
    }
    function doSearch2(value) {
        $('#datagrid2').datagrid('load', {
            cari_temp: value
        });
    }

    $(document).ready(function() {
        $('#datagrid').datagrid({
            onClickRow: function(rowIndex, rowData) {
                if (isOpened) {
                    edit(rowData);
                }
            }
        });
        $('#tabs').tabs({
            onSelect: function(title, index) {
                switch (index) {
                    case 0:
                        $('#datagrid').datagrid('reload');
                        break;
                    case 1:
                        $('#datagrid2').datagrid('reload');
                        break;
                }
            }
        });
    });
</script>

<div class="pageContent">

    <div class="main">

        <div class="container">
            <div align="center">
                <h1>Data Mahasiswa WP-Learning</h1>
                <h2>Anda dapat melakukan manipulasi data mahasiswa di halaman ini</h2>
            </div>
        </div>

        <div id="tabs" class="content easyui-tabs">
            
            <div title="Mahasiswa Mendaftar">

                <div id="tbar2" style="padding:2px;height:30px;">
                    <div style="float:left;">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="accept()">Terima</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="denied()">Tolak</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refresh2()">Refresh</a>
                    </div>
                    <div style="float: right; padding: 2px;">
                        <input id="cari_temp" class="easyui-searchbox" data-options="prompt:' Cari..', searcher:doSearch2" style="width:200px"></input> 
                    </div>
                </div>

                <table id="datagrid2" class="easyui-datagrid" url="controller/msiswa_temp_crud.php?act=get" 
                       toolbar="#tbar" pagination="true" rownumbers="true" fitColumns="true" collapsible="true"
                       checkOnSelect="false" selectOnCheck="false" singleSelect="true" >
                    <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox:true"></th>
                            <th data-options="field:'id'" hidden="true"></th>
                            <th data-options="field:'nim',width:70" sortable="true">NIM</th>
                            <th data-options="field:'nama',width:130" sortable="true">Nama</th>
                            <th data-options="field:'alamat',width:150" sortable="true">Alamat</th>
                            <th data-options="field:'telepon',width:70,align:'center'" sortable="true">Telepon</th>
                            <th data-options="field:'email',width:100,align:'center'" sortable="true">Email</th>
                            <th data-options="field:'tahun',width:50,align:'right'" sortable="true">Angkatan</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div title="Mahasiswa Terdaftar">

                <div id="tbar" style="padding:2px;height:30px;">
                    <div style="float:left;">
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="create()">Tambah</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit(null)">Edit</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroy()">Hapus</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refresh()">Refresh</a>
                    </div>
                    <div style="float: right; padding: 2px;">
                        <input id="cari" class="easyui-searchbox" data-options="prompt:' Cari..', searcher:doSearch" style="width:200px"></input> 
                    </div>
                </div>

                <table id="datagrid" class="easyui-datagrid" url="controller/msiswa_crud.php?act=get" 
                       toolbar="#tbar" pagination="true" rownumbers="true" fitColumns="true" collapsible="true"
                       checkOnSelect="false" selectOnCheck="false" singleSelect="true" >
                    <thead>
                        <tr>
                            <th data-options="field:'ck',checkbox:true"></th>
                            <th data-options="field:'id'" hidden="true"></th>
                            <th data-options="field:'nim',width:50" sortable="true">NIM</th>
                            <th data-options="field:'nama',width:130" sortable="true">Nama</th>
                            <th data-options="field:'alamat',width:170" sortable="true">Alamat</th>
                            <th data-options="field:'telepon',width:70,align:'center'" sortable="true">Telepon</th>
                            <th data-options="field:'email',width:100,align:'center'" sortable="true">Email</th>
                            <th data-options="field:'tahun',width:50,align:'right'" sortable="true">Angkatan</th>
                        </tr>
                    </thead>
                </table>

                <!-- Dialog Form -->
                <div id="dialog-form" closed="true" buttons="#dialog-buttons" resizable="true" class="easyui-dialog" style="width:350px; height:300px; padding: 10px 10px" >
                    <div id="isUpdate" ></div>
                    <form id="form" method="post" novalidate>
                        <table class="data-form">
                            <tr>
                                <td>NIM</td>
                                <td><input type="text" name="nim" id="nim" class="inputbox2 easyui-validatebox" required="true" /></td>
                            </tr>
                            <tr>
                                <td>Nama</td>
                                <td><input type="text" name="nama" id="nama" class="inputbox easyui-validatebox" required="true" /></td>
                            </tr>
                            <tr>
                                <td>Alamat</td>
                                <td><textarea rows="3" class="inputbox easyui-validatebox" name="alamat" id="alamat" ></textarea></td>
                            </tr>
                            <tr>
                                <td>Telepon</td>
                                <td><input type="tel" class="inputbox2 easyui-validatebox" name="telepon" id="telepon" /></td>
                            </tr>
                            <tr>
                                <td>Email</td>
                                <td><input type="email" class="inputbox2 easyui-validatebox" name="email" id="email" /></td>
                            </tr>
                            <tr>
                                <td>Angkatan</td>
                                <td><input type="text" class="inputbox2 easyui-combobox" required="true" name="tahun" id="tahun" 
                                           data-options="url:'ws/angkatan_services.php?action=get', method:'get',
                                           valueField:'Id', textField:'Tahun'">
                                </td>
                            </tr>
                        </table>
                    </form>
                </div>

                <!-- Dialog Button -->
                <div id="dialog-buttons">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">Simpan</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="closeform()">Tutup</a>
                </div>

            </div>

        </div>

        <?php include '_footer.php'; ?>
    </div>
</div>

</body>
</html>